package com.jk.controller;


import com.jk.entity.Cart;
import com.jk.entity.Product;
import com.jk.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
   （购物车Controller）
    宋林轩
 */


@Controller
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;
    
    /**
     * @Author 宋林轩
     * @Description //TODO 查询redis购物车
     * @Date 10:47 2021/3/30
     * @Param [page, rows]
     * @return java.util.List<com.jk.admin.entity.Cart>
     */
    @RequestMapping("queryCartList")
    @ResponseBody
    public List<Cart> queryCartList(HttpServletRequest request){
        List<Cart> list = cartService.queryCartList(request);
        return list;
    }
    
    /**
     * @Author 宋林轩
     * @Description //TODO 添加购物车
     * @Date 9:42 2021/3/31
     * @Param [request, productId, num]
     * @return void
     */
    @RequestMapping("addCart")
    @ResponseBody
    public void addCart(HttpServletRequest request,String productId,Integer num){
        cartService.addCart(request,productId,num);
    }
    
    /**
     * @Author 宋林轩
     * @Description //TODO  测试添加
     * @Date 9:42 2021/3/31
     * @Param [cart]
     * @return void
     */
   /* @RequestMapping("add")
    public void add(Cart cart){
        cartService.add(cart);
    }*/

    /**
     * @Author 宋林轩
     * @Description //TODO 删除
     * @Date 16:44 2021/3/31
     * @Param [id]
     * @return void
     */
    @RequestMapping("del")
    @ResponseBody
    public void del(HttpServletRequest request,String productId){
        cartService.del(request,productId);
    }

    @RequestMapping("productNum")
    @ResponseBody
    public Product productNum(String productId){
        Product product= cartService.productNum(productId);
        return product;
    }
    @RequestMapping("updNum")
    @ResponseBody
    public void updNum(HttpServletRequest request,String productId,Integer num){
        cartService.updNum(request,productId,num);
    }


    @RequestMapping("submitByOrder")
    @ResponseBody
    public void submitByOrder(String[] list, HttpServletRequest request, HttpServletResponse httpResponse,String couponId,String address, String cname, String cphone) throws IOException {
        System.out.println(list);

        cartService.submitOrder(request,list,httpResponse,couponId,cname,cphone,address);
    }
    @RequestMapping("pay")
    public void pay(HttpServletRequest request,HttpServletResponse response) {
        cartService.pay(request);
        try {
            response.sendRedirect("../index.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
